(function() {

this.codium = this.codium || {};
this.codium.panel = this.codium.panel || {};

this.codium.panel.People = codium.panel.BasicPanel.extend(
{
	init : function() 
	{
		this._super();
		
		this.setIcon(this.libDir + "/img/icon/menuPeople.png");
		this.setCaption("Project People");
		
		this.service = new codium.CodiumService();
		
		this.pnlPeople = new pasta.webgui.ContainerControl();
		this.pnlPeople.setLeft(10);
		this.pnlPeople.setTop(40);
		this.pnlPeople.setWidth(this.getWidth() - (2 * this.pnlPeople.getLeft()));
		this.pnlPeople.setHeight(this.getHeight() - this.pnlPeople.getTop() - this.pnlPeople.getLeft());
		this.pnlPeople.setBorderColor("#FFFFFF");
		this.pnlPeople.setBgColor("#FEFEFE");
		this.pnlPeople.setShadow("0px 2px 4px #000000");
		this.addControl(this.pnlPeople);
		
			this.lblPeopleCaption = new pasta.webgui.Label();
			this.lblPeopleCaption.setLeft(5);
			this.lblPeopleCaption.setTop(5);
			this.lblPeopleCaption.setWidth(200);
			this.lblPeopleCaption.setCaption("Project People :");
			this.lblPeopleCaption.font.setBold(true);
			this.lblPeopleCaption.font.setSize(12);
			this.pnlPeople.addControl(this.lblPeopleCaption);
			
			this.shpPeopleCaption = new pasta.webgui.Shape();
			this.shpPeopleCaption.setLeft(this.lblPeopleCaption.getLeft());
			this.shpPeopleCaption.setTop(this.lblPeopleCaption.getTop() + 20);
			this.shpPeopleCaption.setWidth(this.pnlPeople.getWidth() - (2 * this.shpPeopleCaption.getLeft()));
			this.shpPeopleCaption.setMode(2);
			this.pnlPeople.addControl(this.shpPeopleCaption);
			
			this.list = new codium.control.PeopleList();
			this.list.setLeft(15);
			this.list.setTop(35);
			this.list.setWidth(this.pnlPeople.getWidth() - (2 * this.list.getLeft()));
			this.list.setHeight(this.pnlPeople.getHeight() - this.list.getTop() - 50);
			this.pnlPeople.addControl(this.list);
			
			this.btnNew = new pasta.webgui.Button();
			this.btnNew.setLeft(this.list.getLeft());
			this.btnNew.setTop(this.list.getTop() + this.list.getHeight() + 5);
			this.btnNew.setCaption("New");
			this.btnNew.setWidth(80);
			this.btnNew.getOnClick().setHandler(this, this.btnNewClick);
			this.pnlPeople.addControl(this.btnNew);
			
			this.btnEdit = new pasta.webgui.Button();
			this.btnEdit.setLeft(this.btnNew.getLeft() + this.btnNew.getWidth() + 3);
			this.btnEdit.setTop(this.btnNew.getTop());
			this.btnEdit.setCaption("Edit");
			this.btnEdit.setWidth(80);
			this.btnEdit.getOnClick().setHandler(this, this.btnEditClick);
			this.pnlPeople.addControl(this.btnEdit);
			
			this.btnDel = new pasta.webgui.Button();
			this.btnDel.setLeft(this.btnEdit.getLeft() + this.btnEdit.getWidth() + 3);
			this.btnDel.setTop(this.btnEdit.getTop());
			this.btnDel.setCaption("Delete");
			this.btnDel.setWidth(80);
			this.btnDel.getOnClick().setHandler(this, this.btnDelClick);
			this.pnlPeople.addControl(this.btnDel);
			
			this.btnRefresh = new pasta.webgui.Button();
			this.btnRefresh.setLeft(this.btnDel.getLeft() + this.btnDel.getWidth() + 3);
			this.btnRefresh.setTop(this.btnDel.getTop());
			this.btnRefresh.setCaption("Refresh");
			this.btnRefresh.setWidth(80);
			this.btnRefresh.getOnClick().setHandler(this, this.btnRefreshClick);
			this.pnlPeople.addControl(this.btnRefresh);
		
		this.userList = new Array();
	},	
	
	initPanel : function()
	{
		if (parseInt(app.userObj.roleId) == 1)
		{
			this.btnNew.setVisible(true);
			this.btnEdit.setVisible(true);
			this.btnDel.setVisible(true);
		}
		else
		{
			this.btnNew.setVisible(false);
			this.btnEdit.setVisible(false);
			this.btnDel.setVisible(false);
		}
		
		this.refresh();
	},
	
	refresh : function()
	{
		var target = this;
		
		this.service.listUser(
			function(result)
			{
				target.updateList(result);
			});
	},
	
	updateList : function(list)
	{
		this.userList = list;
		this.list.clear();
		
		for (var i = 0; i < list.length; i++)
		{
			var obj = list[i];
			
			this.list.addItem(obj.userId, obj.userName, obj.email, obj.roleId);
		}
	},
	
	//-------------------- Service Event Handler --------------
	
	btnRefreshClick : function(sender)
	{
		this.refresh();
	},
	
	btnNewClick : function(sender)
	{
		if (this.pnlNewPeople == null)
		{
			this.pnlNewPeople = new codium.panel.NewPeople();
			this.pnlNewPeople.getOnOK().setHandler(this, this.pnlNewPeopleOK);
			this.pnlNewPeople.setWidth(stage.getWidth());
			this.pnlNewPeople.setHeight(stage.getHeight());
			stage.addControl(this.pnlNewPeople);
		}
		
		this.pnlNewPeople.show();
	},
	
	pnlNewPeopleOK : function(sender, userId, userName, pass, email, role)
	{
		var target = this;
		
		if (sender.mode == 1)
		{
			this.service.addUser(userId, userName, pass, email, role,
				function(result)
				{
					target.refresh();
				});
		}
		else
		{
			this.service.updateUser(userId, userName, pass, email, role,
				function(result)
				{
					target.refresh();
				});
		}
	},
	
	btnDelClick : function(sender)
	{
		if (this.list.getSelectedItem() != null)
		{
			var ret = confirm("Are you sure to delete user " + this.list.getSelectedItem().userName + " ? ");
			
			if (ret)
			{
				var target = this;
				
				this.service.delUser(this.list.getSelectedItem().id, 
					function(result)
					{
						target.refresh();
					});
			}
		}
	},
	
	btnEditClick : function(sender)
	{
		if (this.list.getSelectedItem() != null)
		{
			var user = null;
			
			for (var i = 0; i < this.userList.length; i++)
			{
				var obj = this.userList[i];
				
				if (obj.userId == this.list.getSelectedItem().id)
				{
					user = obj;
					break;
				}
			}
			
			if (user != null)
			{
				if (this.pnlNewPeople == null)
				{
					this.pnlNewPeople = new codium.panel.NewPeople();
					this.pnlNewPeople.getOnOK().setHandler(this, this.pnlNewPeopleOK);
					this.pnlNewPeople.setWidth(stage.getWidth());
					this.pnlNewPeople.setHeight(stage.getHeight());
					stage.addControl(this.pnlNewPeople);
				}
				
				this.pnlNewPeople.showEdit(user);
			}
		}
	},
	
	setWidth : function(data)
	{
		this._super(data);
		
		if (this.pnlPeople != null)
		{
			this.pnlPeople.setWidth(this.getWidth() - (2 * this.pnlPeople.getLeft()));
			
				this.shpPeopleCaption.setWidth(this.pnlPeople.getWidth() - (2 * this.shpPeopleCaption.getLeft()));
				this.list.setWidth(this.pnlPeople.getWidth() - (2 * this.list.getLeft()));
		}
	},
	
	setHeight : function(data)
	{
		this._super(data);
		
		if (this.pnlPeople != null)
		{
			this.pnlPeople.setHeight(this.getHeight() - this.pnlPeople.getTop() - this.pnlPeople.getLeft());
			
				this.list.setHeight(this.pnlPeople.getHeight() - this.list.getTop() - 50);
				this.btnNew.setTop(this.list.getTop() + this.list.getHeight() + 5);
				this.btnEdit.setTop(this.btnNew.getTop());
				this.btnDel.setTop(this.btnEdit.getTop());
				this.btnRefresh.setTop(this.btnEdit.getTop());
		}
	},
	
});

})();